package com.ruoyi.project.ledger.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.data.domain.WeightStation;
import com.ruoyi.project.data.mapper.WeightStationMapper;
import com.ruoyi.project.ledger.domain.LedgerFile;
import com.ruoyi.project.ledger.mapper.LedgerFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.ledger.mapper.LedgerSpecialActivityMapper;
import com.ruoyi.project.ledger.domain.LedgerSpecialActivity;
import com.ruoyi.project.ledger.service.ILedgerSpecialActivityService;

/**
 * 专项活动Service业务层处理
 *
 * @author ruoyi
 * @date 2020-06-14
 */
@Service
public class LedgerSpecialActivityServiceImpl implements ILedgerSpecialActivityService
{
    @Autowired
    private LedgerSpecialActivityMapper ledgerSpecialActivityMapper;
    @Autowired
    private LedgerFileMapper ledgerFileMapper;
    @Autowired
    private WeightStationMapper weightStationMapper;

    /**
     * 查询专项活动
     *
     * @param id 专项活动ID
     * @return 专项活动
     */
    @Override
    public LedgerSpecialActivity selectLedgerSpecialActivityById(Long id)
    {
        LedgerSpecialActivity specialActivity = ledgerSpecialActivityMapper.selectLedgerSpecialActivityById(id);
        if(specialActivity != null){
            // 获取文件
            LedgerFile ledgerFile = new LedgerFile();
            ledgerFile.setLedgerId(id);
            ledgerFile.setLedgerType("9");      // 专项活动
            ledgerFile.setFileName("1");      // 活动方案
            List<Map<String, Object>> imgList = ledgerFileMapper.selectLedgerImgList2(ledgerFile);
            if (imgList != null && imgList.size() >0){
                for (Map<String, Object> map : imgList){
                    map.put("url2",map.get("url"));
                    map.put("url","http://ycsjoss.oss-cn-hangzhou.aliyuncs.com/weixiu/2020/file/06/89d21521cd4b4e208b1a7f61f39a63bf.jpg");
                }
            }
            specialActivity.setFileList(imgList);
            ledgerFile.setFileName("2"); // 活动开展
            List<Map<String, Object>> imgList2 = ledgerFileMapper.selectLedgerImgList2(ledgerFile);
            if (imgList2 != null && imgList2.size() >0){
                for (Map<String, Object> map : imgList2){
                    map.put("url2",map.get("url"));
                    map.put("url","http://ycsjoss.oss-cn-hangzhou.aliyuncs.com/weixiu/2020/file/06/89d21521cd4b4e208b1a7f61f39a63bf.jpg");
                }
            }
            specialActivity.setFileList2(imgList2);
            ledgerFile.setFileName("3"); // 活动总结
            List<Map<String, Object>> imgList3 = ledgerFileMapper.selectLedgerImgList2(ledgerFile);
            if (imgList3 != null && imgList3.size() >0){
                for (Map<String, Object> map : imgList3){
                    map.put("url2",map.get("url"));
                    map.put("url","http://ycsjoss.oss-cn-hangzhou.aliyuncs.com/weixiu/2020/file/06/89d21521cd4b4e208b1a7f61f39a63bf.jpg");
                }
            }
            specialActivity.setFileList3(imgList3);
        }
        return specialActivity;
    }
    private List<Map<String, Object>> returnList(List<Map<String, Object>> imgList){
        if (imgList != null && imgList.size() >0){
            for (Map<String, Object> map : imgList){
                map.put("url2",map.get("url"));
                map.put("url","http://ycsjoss.oss-cn-hangzhou.aliyuncs.com/weixiu/2020/file/06/89d21521cd4b4e208b1a7f61f39a63bf.jpg");
            }
            return imgList;
        }
        return (List<Map<String, Object>>) new HashMap<Object, Object>();
    }

    /**
     * 查询专项活动列表
     *
     * @param ledgerSpecialActivity 专项活动
     * @return 专项活动
     */
    @Override
    public List<LedgerSpecialActivity> selectLedgerSpecialActivityList(LedgerSpecialActivity ledgerSpecialActivity)
    {
        WeightStation station = weightStationMapper.selectWeightStationById(ledgerSpecialActivity.getStationId());
        List<LedgerSpecialActivity> specialActivities = ledgerSpecialActivityMapper.selectLedgerSpecialActivityList2(ledgerSpecialActivity);
            for (LedgerSpecialActivity a : specialActivities){
                if (StringUtils.isBlank(a.getStationName()) && station !=null){
                    a.setStationName(station.getStationName());
                }
            }
        return specialActivities;
    }


    /**
     * 新增专项活动
     *
     * @param ledgerSpecialActivity 专项活动
     * @return 结果
     */
    @Override
    public int insertLedgerSpecialActivity(LedgerSpecialActivity ledgerSpecialActivity)
    {
        ledgerSpecialActivity.setCreateTime(DateUtils.getNowDate());
        int m = ledgerSpecialActivityMapper.insertLedgerSpecialActivity(ledgerSpecialActivity);
        int i = 0;
        int j = 0;
        int k = 0;
        if(ledgerSpecialActivity.getFiles() != null && ledgerSpecialActivity.getFiles().length > 0){
            for(String path : ledgerSpecialActivity.getFiles()){
                LedgerFile ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("9");
                ledgerFile.setFileName("1");
                ledgerFile.setLedgerId(ledgerSpecialActivity.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerSpecialActivity.getCreateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
                i++;
            }
        }
        if(ledgerSpecialActivity.getFiles2() != null && ledgerSpecialActivity.getFiles2().length > 0){
            for(String path : ledgerSpecialActivity.getFiles2()){
                LedgerFile ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("9");
                ledgerFile.setFileName("2");
                ledgerFile.setLedgerId(ledgerSpecialActivity.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerSpecialActivity.getCreateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
                j++;
            }
        }
        if(ledgerSpecialActivity.getFiles3() != null && ledgerSpecialActivity.getFiles3().length > 0){
            for(String path : ledgerSpecialActivity.getFiles3()){
                LedgerFile ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("9");
                ledgerFile.setFileName("3");
                ledgerFile.setLedgerId(ledgerSpecialActivity.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerSpecialActivity.getCreateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
                k++;
            }
        }
        if (i >0 && j>0 && k >0){
            ledgerSpecialActivity.setStatus("1");
            ledgerSpecialActivityMapper.updateLedgerSpecialActivity(ledgerSpecialActivity);
        }
        return m;
    }

    /**
     * 修改专项活动
     *
     * @param ledgerSpecialActivity 专项活动
     * @return 结果
     */
    @Override
    public int updateLedgerSpecialActivity(LedgerSpecialActivity ledgerSpecialActivity)
    {
        ledgerSpecialActivity.setUpdateTime(DateUtils.getNowDate());
        // 删除台账文件
        LedgerFile ledgerFile = new LedgerFile();
        ledgerFile.setLedgerId(ledgerSpecialActivity.getId());
        ledgerFile.setLedgerType("9");
        ledgerFileMapper.deleteLedgerImg(ledgerFile);
        int i = 0;
        int j = 0;
        int k = 0;
        if(ledgerSpecialActivity.getFiles() != null && ledgerSpecialActivity.getFiles().length > 0){
            for(String path : ledgerSpecialActivity.getFiles()){
                ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("9");
                ledgerFile.setFileName("1");
                ledgerFile.setLedgerId(ledgerSpecialActivity.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerSpecialActivity.getCreateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
                i++;
            }
        }
        if(ledgerSpecialActivity.getFiles2() != null && ledgerSpecialActivity.getFiles2().length > 0){
            for(String path : ledgerSpecialActivity.getFiles2()){
                ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("9");
                ledgerFile.setFileName("2");
                ledgerFile.setLedgerId(ledgerSpecialActivity.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerSpecialActivity.getCreateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
                j++;
            }
        }
        if(ledgerSpecialActivity.getFiles3() != null && ledgerSpecialActivity.getFiles3().length > 0){
            for(String path : ledgerSpecialActivity.getFiles3()){
                ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("9");
                ledgerFile.setFileName("3");
                ledgerFile.setLedgerId(ledgerSpecialActivity.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerSpecialActivity.getCreateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
                k++;
            }
        }
        if (i >0 && j>0 && k >0){
            ledgerSpecialActivity.setStatus("1");
        }else {
            ledgerSpecialActivity.setStatus("0");
        }
        return ledgerSpecialActivityMapper.updateLedgerSpecialActivity(ledgerSpecialActivity);
    }

    /**
     * 批量删除专项活动
     *
     * @param ids 需要删除的专项活动ID
     * @return 结果
     */
    @Override
    public int deleteLedgerSpecialActivityByIds(Long[] ids)
    {
        int i = ledgerSpecialActivityMapper.deleteLedgerSpecialActivityByIds(ids);
        for(Long id : ids){
            LedgerFile ledgerFile = new LedgerFile();
            ledgerFile.setLedgerId(id);
            ledgerFile.setLedgerType("9");
            ledgerFileMapper.deleteLedgerImg(ledgerFile);
        }
        return i;
    }

    /**i
     * 删除专项活动信息
     *
     * @param id 专项活动ID
     * @return 结果
     */
    @Override
    public int deleteLedgerSpecialActivityById(Long id)
    {
        int i = ledgerSpecialActivityMapper.deleteLedgerSpecialActivityById(id);
        LedgerFile ledgerFile = new LedgerFile();
        ledgerFile.setLedgerId(id);
        ledgerFile.setLedgerType("9");
        ledgerFileMapper.deleteLedgerImg(ledgerFile);
        return i;
    }
}
